package com.ruoyi.deal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.deal.domain.HyDealCategory;
import com.ruoyi.deal.bo.HyDealCategoryAddBo;
import com.ruoyi.deal.bo.HyDealCategoryEditBo;
import com.ruoyi.deal.bo.HyDealCategoryQueryBo;
import com.ruoyi.deal.domain.HyDealBrand;
import com.ruoyi.deal.mapper.HyDealBrandMapper;
import com.ruoyi.deal.mapper.HyDealCategoryBrandMapper;
import com.ruoyi.deal.mapper.HyDealCategoryMapper;
import com.ruoyi.deal.service.IHyDealCategoryService;
import com.ruoyi.deal.vo.HyDealCategoryVo;
import com.ruoyi.deal.vo.TreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分类管理Service业务层处理
 *
 * @author lixin
 * @date 2021-05-13
 */
@Service
public class HyDealCategoryServiceImpl extends ServicePlusImpl<HyDealCategoryMapper, HyDealCategory> implements IHyDealCategoryService {


	@Override
	public HyDealCategoryVo queryById(Integer categoryId) {
		HyDealCategory db = this.baseMapper.selectById(categoryId);
		return BeanUtil.toBean(db, HyDealCategoryVo.class);
	}

	@Override
	public List<HyDealCategoryVo> queryList(HyDealCategoryQueryBo bo) {

		return listVo(buildQueryWrapper(bo), HyDealCategoryVo.class);
	}

	/**
	 * 查询参数拼接
	 *
	 * @param bo
	 * @return
	 */
	private LambdaQueryWrapper<HyDealCategory> buildQueryWrapper(HyDealCategoryQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<HyDealCategory> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getCategoryName()), HyDealCategory::getCategoryName, bo.getCategoryName());
		lqw.eq(bo.getParentId() != null, HyDealCategory::getParentId, bo.getParentId());
		lqw.eq(StrUtil.isNotBlank(bo.getCategoryPic()), HyDealCategory::getCategoryPic, bo.getCategoryPic());
		lqw.eq(bo.getOrderNum() != null, HyDealCategory::getOrderNum, bo.getOrderNum());
		lqw.eq(bo.getStatus() != null, HyDealCategory::getStatus, bo.getStatus());
		lqw.eq(bo.getCategoryLevel() != null, HyDealCategory::getCategoryLevel, bo.getCategoryLevel());

		lqw.orderByAsc(HyDealCategory::getParentId, HyDealCategory::getOrderNum);
		return lqw;
	}


	/**
	 * 构建前端所需要下拉树结构
	 *
	 * @param categories 分类列表
	 * @return 下拉树结构列表
	 */
	@Override
	public List<TreeSelect> buildCategoryTreeSelect(List<HyDealCategory> categories) {
		List<HyDealCategory> categoryTrees = buildCategoryTree(categories);
		return categoryTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
	}

	/**
	 * 构建前端所需要树结构
	 *
	 * @param categories 分类列表
	 * @return 树结构列表
	 */
	@Override
	public List<HyDealCategory> buildCategoryTree(List<HyDealCategory> categories) {
		List<HyDealCategory> returnList = new ArrayList<HyDealCategory>();
		List<Integer> tempList = new ArrayList<Integer>();
		for (HyDealCategory category : categories) {
			tempList.add(category.getCategoryId());
		}
		for (Iterator<HyDealCategory> iterator = categories.iterator(); iterator.hasNext(); ) {
			HyDealCategory category = (HyDealCategory) iterator.next();
			// 如果是顶级节点, 遍历该父节点的所有子节点
			if (!tempList.contains(category.getParentId())) {
				recursionFn(categories, category);
				returnList.add(category);
			}
		}
		if (returnList.isEmpty()) {
			returnList = categories;
		}
		return returnList;
	}

	@Override
	public List<Integer> selectCategoryListByBrandId(Long brandId) {
		return baseMapper.selectCategoryListByBrandId(brandId);
	}

	/**
	 * 获取分类右边列表
	 * @return
	 */
	@Override
	public List<HyDealCategory> selectLeft() {
		LambdaQueryWrapper<HyDealCategory> lqw = Wrappers.lambdaQuery();
		lqw.eq(HyDealCategory::getParentId, 0);
		lqw.eq(HyDealCategory::getCategoryLevel, 1);
		lqw.orderByAsc(HyDealCategory::getOrderNum);
		return this.list(lqw);
	}


	/**
	 * 根据父类ID获取子类右边列表
	 * @return
	 */
	@Override
	public List<HyDealCategory> selectRight(Integer categoryId) {
		LambdaQueryWrapper<HyDealCategory> lqw = Wrappers.lambdaQuery();
		lqw.eq(HyDealCategory::getParentId, categoryId);
		lqw.orderByAsc(HyDealCategory::getOrderNum);
		return this.list(lqw);
	}

	@Override
	public Boolean insertByAddBo(HyDealCategoryAddBo bo) {
		HyDealCategory add = BeanUtil.toBean(bo, HyDealCategory.class);

		// 设置分类等级
		setCategoryLevel(add);

		validEntityBeforeSave(add);
		return this.save(add);
	}

	@Override
	public Boolean updateByEditBo(HyDealCategoryEditBo bo) {
		HyDealCategory update = BeanUtil.toBean(bo, HyDealCategory.class);

		// 设置分类等级
		setCategoryLevel(update);

		validEntityBeforeSave(update);
		return this.updateById(update);
	}

	@Override
	public Boolean deleteWithValidById(Integer categoryId, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return this.removeById(categoryId);
	}


	/**
	 * 是否存在子节点
	 *
	 * @param categoryId 分类ID
	 * @return 结果
	 */
	@Override
	public boolean hasChildByCategoryId(Integer categoryId) {

		int result = count(new LambdaQueryWrapper<HyDealCategory>().eq(HyDealCategory::getParentId, categoryId));
		return result > 0 ? true : false;
	}

	/**
	 * 设置分类等级
	 *
	 * @param category
	 * @return
	 */
	private void setCategoryLevel(HyDealCategory category) {
		Integer parentId = category.getParentId();
		if (parentId != 0) {
			Integer parentCategoryLevel = this.queryById(parentId).getCategoryLevel();
			category.setCategoryLevel(parentCategoryLevel + 1);
		}
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(HyDealCategory entity) {
		//TODO 做一些数据校验,如唯一约束
	}


	/**
	 * 递归列表
	 *
	 * @param list
	 * @param t
	 */
	private void recursionFn(List<HyDealCategory> list, HyDealCategory t) {
		// 得到子节点列表
		List<HyDealCategory> childList = getChildList(list, t);
		t.setChildren(childList);
		for (HyDealCategory tChild : childList) {
			if (hasChild(list, tChild)) {
				recursionFn(list, tChild);
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<HyDealCategory> getChildList(List<HyDealCategory> list, HyDealCategory t) {
		List<HyDealCategory> tlist = new ArrayList<HyDealCategory>();
		Iterator<HyDealCategory> it = list.iterator();
		while (it.hasNext()) {
			HyDealCategory n = (HyDealCategory) it.next();
			if (n.getParentId().longValue() == t.getCategoryId().longValue()) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<HyDealCategory> list, HyDealCategory t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}
}
